package cn.imvc.app.controllers.order;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.domains.IPayChannel;
import cn.imvc.app.domains.OrderState;
import cn.imvc.app.domains.OrderType;
import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsSku;
import cn.imvc.app.entities.Member;
import cn.imvc.app.entities.Orders;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.app.models.MemberModel;
import cn.imvc.app.models.OrdersModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

public class OrdersControllerImpl implements OrdersController {
	static Logger logger = Logger.getLogger(OrdersControllerImpl.class);
	@Autowired
	OrdersModel ordersModel;
	@Autowired
	MemberModel memberModel;
	@Autowired
	LoggingModel loggingModel;
	
	public Page<Orders> page;
	public Page<Object[]> pageObj;
	public Goods goods;
	public Orders orders;
	public ViewRequest viewRequest;
	public int id;
	public int[] ids;
	public Map<Byte, String> orderState = OrderState.elMap;
	public Map<Byte, String> payChannel = IPayChannel.elMap;
	private int memberId;
	private Member member;
	public String memberName;
	
	public void setPageObj(Page<Object[]> pageObj) {
		this.pageObj = pageObj;
	}

	public void setMemberName(String memberName) {
		this.memberName = memberName;
	}

	public void setPage(Page<Orders> page) {
		this.page = page;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public void setOrders(Orders orders) {
		this.orders = orders;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}
	
	/**
	 * 通过会员id查找订单
	 */
	public String search() {
		if(viewRequest == null){
			viewRequest = new ViewRequest();
		}
			viewRequest.getParams().put("_and_o_dot_member_dot_id_eq_number",""+memberId);
			page=ordersModel.find(viewRequest);
			member = memberModel.getOne(memberId);
		return "orders/search";
	}

	public String index() {
		pageObj=ordersModel.findPageObjects(viewRequest);
		return "orders/index";
	}

	public String show() {
		// TODO Auto-generated method stub
		return null;
	}

	public String editNew() {
		// TODO Auto-generated method stub
		return null;
	}

	public String create() {
		// TODO Auto-generated method stub
		return null;
	}

	public String edit() {
		orders=ordersModel.get(id);
		return "orders/edit";
	}

	public String update() {
		// TODO Auto-generated method stub
		return null;
	}

	public String destroy() {
		// TODO Auto-generated method stub
		return null;
	}

	public String destroyBatch() {
		// TODO Auto-generated method stub
		return null;
	}

	public String adminCancel() {
		if(ordersModel.updateAdminCancel(id)){
			loggingModel.info(logger, "驳回订单："+id);
			return Render.json(new DialogBean("200", "驳回成功","orders_show"));
		}else{
			return Render.json(new DialogBean("300", "驳回失败，原因可能是订单状态异常！","orders_show"));
		}
	}

	public int getMemberId() {
		return memberId;
	}

	public void setMemberId(int memberId) {
		this.memberId = memberId;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	/**
	 * 将已付款订单变更为待出库。因为系统中不存在"订单确认"状态，所以需要将订单状态改为"待出库"
	 */
	public String confirm() {
		if(ordersModel.updateConfirm(id) == null){
			return Render.json(new DialogBean("300", "该订单状态异常，确认失败","orders_show"));
		}else{
			loggingModel.info(logger, "确认订单："+id);
			return Render.json(new DialogBean("200", "确认成功","orders_show"));
		}
	}

	public String confirmBatch() {
		ordersModel.updateBatchConfirm(ids);
		return Render.json(new DialogBean("200", "批量确认成功",""));
	}
	
	//进入订单维护界面
	public String mana() {
		orders=ordersModel.get(id);
		return "orders/mana";
	}
	
	//强制驳回该订单，用于订单确认状态时，缺货或其他原因，将该订单强制修改为驳回状态
	public String doCancel() {
		if(ordersModel.updateDoCancel(id)){
			loggingModel.info(logger, "强制驳回订单："+id);
			return Render.json(new DialogBean("200", "强制驳回成功","orders_show"));
		}else{
			return Render.json(new DialogBean("300", "强制驳回失败，原因可能是订单状态异常！","orders_show"));
		}
	}
}
